package com.gulimall.product.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author Rookie-6688
 * @Description
 * @Create 2021-04-01 17:05
 */
@EnableConfigurationProperties(CacheProperties.class)       // 将读取配置缓存的类CacheProperties加入Spring容器（因为默认没用加到容器中，所以这里需要加入容器）
@Configuration
@EnableCaching      // 开启缓存配置
public class MyCacheConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){    // CacheProperties 会自动从容器中获取到
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        // 设置 key 序列化（默认String）
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                // 设置 value 序列化（默认JDK）
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));


        // 默认在自定义 RedisConfiguration 后会不使用配置文件中配置，所以我们需要额外设置配置文件中的配置，也就是读取配置文件中的配置赋值到配置对象中
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        //将配置文件中所有的配置都生效
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());      // 存活时间
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());     // 缓存 key 前缀
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();             // 是否缓存空值
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();             // 是否启用 key 前缀
        }

        return config;
    }

    // 配置CacheManager
//    @Bean
//    public CacheManager cacheManager(RedisConnectionFactory factory, CacheProperties cacheProperties) {
//
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//        // config = config.entryTtl();
//        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
//        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//
//        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
//        //将配置文件中所有的配置都生效
//        if (redisProperties.getTimeToLive() != null) {
//            config = config.entryTtl(redisProperties.getTimeToLive());
//        }
//        if (redisProperties.getKeyPrefix() != null) {
//            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
//        }
//        if (!redisProperties.isCacheNullValues()) {
//            config = config.disableCachingNullValues();
//        }
//        if (!redisProperties.isUseKeyPrefix()) {
//            config = config.disableKeyPrefix();
//        }
//
//        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
//                .cacheDefaults(config)
//                .build();
//
//        return cacheManager;
//    }
}
